//
//  BQTabBarController.m
//  BeeQuick
//
//  Created by Vivitickey on 2017/4/26.
//  Copyright © 2017年 test. All rights reserved.
//

#import "BQTabBarController.h"
#import "BQNavigationController.h"
#import "BQShopCartViewController.h"
#import "BQTabBar.h"
@interface BQTabBarController ()<UITabBarControllerDelegate>
    
    @end

@implementation BQTabBarController
    
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setValue:[BQTabBar new] forKey:@"tabBar"];
    
    NSMutableArray *mutable = [NSMutableArray array];
    
    [mutable addObject:[self addChildControllerWith:@"BQHomeViewController" title:@"首页" normalImage:@"v2_home"]];
    [mutable addObject:[self addChildControllerWith:@"BQOrderViewController" title:@"闪送超市" normalImage:@"v2_order"]];
    [mutable addObject:[self addChildControllerWith:@"BQShopCartViewController" title:@"购物车" normalImage:@"shopCart"]];
    [mutable addObject:[self addChildControllerWith:@"BQMyViewController" title:@"我的" normalImage:@"v2_my"]];
    
    self.viewControllers  = mutable;
    
    self.delegate = self;
    
}
    
    
    /**
     自定义创建子控制器方法
     */
-(BQNavigationController *)addChildControllerWith:(NSString *)clsName title:(NSString *)title normalImage:(NSString *)normalImage{
    
    Class cls = NSClassFromString(clsName);
    
    UIViewController *controller = [[cls alloc]init];
    
    controller.title = title;
    
    controller.tabBarItem.image = [UIImage imageNamed:normalImage];
    
    NSString *selImage = [normalImage stringByAppendingString:@"_selected"];
    
    UIImage *image = [[UIImage imageNamed:selImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    controller.tabBarItem.selectedImage = image;
    
    [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor]} forState:UIControlStateSelected];
    
    BQNavigationController *nav = [[BQNavigationController alloc]initWithRootViewController:controller];
    
    return nav;
    
    
}

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    
    
    BQNavigationController *temp = (BQNavigationController *)viewController;
    
    if ([[temp.viewControllers objectAtIndex:0] isKindOfClass:[BQShopCartViewController class]]) {
        
        BQShopCartViewController *ff = [[BQShopCartViewController alloc] init];
        
        BQNavigationController *nav = [[BQNavigationController alloc]initWithRootViewController:ff];
        
        [self presentViewController:nav animated:YES completion:nil];
        

        return NO;
    }
    return YES;
    
}


    
    @end
